package com.ruoyi.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.app.domain.AppArticleMine;
import com.ruoyi.app.service.AppArticleMineService;
import com.ruoyi.app.service.AppArticleService;
import com.ruoyi.app.domain.AppArticle;
import com.ruoyi.cms.domain.Category;
import com.ruoyi.cms.domain.Comment;
import com.ruoyi.cms.service.ICategoryService;
import com.ruoyi.cms.service.ICommentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.service.ISysNoticeService;
import jdk.nashorn.internal.parser.JSONParser;
import netscape.javascript.JSObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/app/")
public class AppIndexController extends BaseController {

    @Autowired
    private ICommentService commentService;

    @Autowired
    private AppArticleService articleService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private AppArticleMineService appArticleMineService;

    @Autowired
    private ISysNoticeService sysNoticeService;

    //首页
    @RequestMapping("index")
    public String goIndex(Model model) {
        Map<String,Object> user = JSONObject.parseObject(ShiroUtils.getSysUser().getRemark());
        user.put("name",ShiroUtils.getSysUser().getUserName());
        AppArticle article = new AppArticle();
        Comment comment =new Comment();
        comment.setUserId(ShiroUtils.getUserId().toString());
        List<AppArticle> list = articleService.selectArticleList(article);//文章
        List<Comment> commentList = commentService.selectComments(comment);
        List<SysNotice> notices= sysNoticeService.selectNoticeList(new SysNotice());
        List<AppArticleMine> dzList = getList(1);//点赞
        List<AppArticleMine> bmList = getList(2);//报名 已签到

        model.addAttribute("notices", notices);
        model.addAttribute("list", list);
        model.addAttribute("commentList", commentList);
        model.addAttribute("bmList", bmList);
        model.addAttribute("dzList", dzList);
        model.addAttribute("user", user);

        return "appIndex";
    }

    private List<AppArticleMine> getList(int type) {
        int userId = Integer.parseInt(ShiroUtils.getSysUser().getUserId().toString());
        AppArticleMine aMin = new AppArticleMine();
        aMin.setType(type);
        aMin.setUserId(userId);
        List<AppArticleMine> list = appArticleMineService.queryArticleMineList(aMin);
        return list;
    }

    //详情
    @RequestMapping("detail/{articleId}")
    public String goDetail(Model model, @PathVariable("articleId") String articleId) {
        Comment form=new Comment();
        form.setTid(articleId);
        AppArticle article = articleService.selectArticleById(articleId);
        if (article == null) {
            throw new BusinessException("该文章不存在!");
        }
        List<Comment> commentList = commentService.selectComments(form);
        model.addAttribute("article", article);
        model.addAttribute("list", commentList);
        model.addAttribute("categoryId", article.getCategoryId());
        return "appDetail";
    }

    //详情
    @RequestMapping("list/{categoryId}")
    public String goList(@PathVariable("categoryId") String categoryId, Model model) {
        Category category = categoryService.selectCategoryById(Long.valueOf(categoryId));
        if (category != null) {
            model.addAttribute("categoryName", category.getCategoryName());
        }
        AppArticle form = new AppArticle();
        form.setCategoryId(categoryId);
        model.addAttribute("categoryId", categoryId);
        startPage();
        List<AppArticle> list = articleService.selectArticleList(form);
        model.addAttribute("list", list);
        return "appList";
    }
    //问卷
    @RequestMapping("wenjuan")
    public String goWenjuan(Model model) {

        return "wenjuan";
    }


}
